package net.bxx2004.pandalib.bukkit.file;

import com.alibaba.fastjson.JSONObject;
import net.bxx2004.pandalib.PandaLib;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.*;


public class JSONData {
    private File dataPath;
    public JSONData(File path){
        this.dataPath = path;
        if (!dataPath.exists()){
            dataPath.mkdirs();
        }
    }
    private File dataFile(String dataName){
        for (File file : dataPath.listFiles()){
            if (file.getName().replaceAll("\\.json","").equalsIgnoreCase(dataName)){
                return file;
            }
        }
        return null;
    }
    public void delete(String dataName){
        dataFile(dataName).delete();
    }
    public void create(String dataName,String json){
        File file = null;
        if (dataFile(dataName) == null){
            file = new File(dataPath.getAbsolutePath() + "/" + dataName + ".json");
            try {
                file.createNewFile();
                FileWriter fileWritter = new FileWriter(file,true);
                PrintWriter printWriter = new PrintWriter(fileWritter, true);
                printWriter.print(json);
                fileWritter.close();
                printWriter.close();
                new BukkitRunnable(){
                    @Override
                    public void run() {
                        try {
                            printWriter.print(json);
                            fileWritter.close();
                            printWriter.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.runTaskAsynchronously(PandaLib.initPlugin);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public JSONObject data(String dataName){
        try {
            return JSONObject.parseObject(reader(dataName));
        } catch (Exception e) {

        }
        return null;
    }
    public void set(String dataName,JSONObject object){
        try {
            FileWriter fileWriter = new FileWriter(dataFile(dataName), false);
            PrintWriter printWriter = new PrintWriter(fileWriter, true);
            printWriter.print("");
            printWriter.print(object.toString());
            fileWriter.close();
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private String reader(String dataName){
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile(dataName))));
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null){
                stringBuilder.append(line);
            }
            reader.close();
            return stringBuilder.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
